﻿using AgvDispatchSystemWpf.BLL;
using AgvDispatchSystemWpf.Model;
using Communication.Gree;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZwxCommunication;

namespace AgvDispatchSystemWpf.Base
{
    public static class GlobalMonitor
    {
        static Task mainTask = null;
        static GreeAgv AgvDriveInstance = null;

        static bool isRunning = true;

        public static List<DeviceModel> DeviceList { get; set; }

        public static SerialInfo SerialInfo { get; set; }

        
        public static void Start(Action successAction,Action<string> faultAction)
        {
            mainTask = Task.Run(async() =>
            {
                DispatchSystemBll bll = new DispatchSystemBll();
                // 获取串口配置信息
                var si = bll.InitialSerialInfo();
                if (si.State)
                    SerialInfo = si.Data;
                else
                {
                    faultAction(si.Message); return;
                }
                // 初始化设备变量集合及警戒值
                var dr = bll.InitialDevices();
                if (dr.State)
                    DeviceList = dr.Data;
                else
                {
                    faultAction(dr.Message); return;
                }

                AgvDriveInstance = GreeAgv.GetInstance(SerialInfo);
                AgvDriveInstance.ResponseData = new Action<int, List<byte>>(ParsingData);

                if (AgvDriveInstance.Connection())
                {
                    successAction();
                    bool[] agvTarfficControlBits = new bool[56];
                    while (isRunning)
                    {
                        for (int i = 0; i < DeviceList.Count; i++)
                        {
                            agvTarfficControlBits[i] = DeviceList[i].AgvInfo.ControlData.TrafficControl;
                        }
                      await  AgvDriveInstance.Send(0xC1, agvTarfficControlBits.BitArrayToByteArray());
                    }
                }
                else
                {
                    faultAction("程序无法启动，串口连接初始化失败！请检查设备是否连接正常。");
                }
            });
        }


        private static void ParsingData(int device_id,List<byte>byteList)
        {
            var dev = DeviceList.Find(d => d.DeviceId == device_id.ToString());
            if (dev!=null)
            {
                dev.AgvInfo.StateData.GetStateByte = byteList[0];
                dev.AgvInfo.StateData.GetPathByte = byteList[1];
                dev.AgvInfo.StateData.GetRfidNum = BitConverter.ToUInt16(new byte[] { byteList[2], byteList[3] }, 0);
                dev.AgvInfo.StateData.GetSpeedByte = byteList[4];
                dev.AgvInfo.StateData.GetBatteryByte = byteList[5];
                dev.AgvInfo.StateData.GetTaskByte = byteList[6];
            }
        }

        public static void Dispose()
        {
            AgvDriveInstance?.Dispose();
            mainTask?.Wait();
        }
    }
}
